package com.lrkj.property.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Room;
import com.lrkj.property.mapper.UnitMapper;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.property.service.IRoomService;
import com.lrkj.system.service.IOrgTreeService;
import com.lrkj.waitress.domain.Repairs;
import com.lrkj.waitress.mapper.RepairsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.property.mapper.RepairOrderMapper;
import com.lrkj.property.domain.RepairOrder;
import com.lrkj.property.service.IRepairOrderService;

/**
 * 报修单Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-27
 */
@Service
public class RepairOrderServiceImpl implements IRepairOrderService {

    @Autowired
    private RepairOrderMapper repairOrderMapper;

    @Autowired
    private RepairsMapper repairsMapper;

    @Autowired
    private UnitMapper unitMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IRoomService roomService;

    /**
     * 查询报修单
     *
     * @param id 报修单ID
     * @return 报修单
     */
    @Override
    public RepairOrder selectRepairOrderById(String id) {
        RepairOrder repairOrder = repairOrderMapper.selectRepairOrderById(id);
        if(StringUtils.isEmpty(repairOrder.getOwnerName())){
            repairOrder.setOwnerName(repairOrder.getRepairPerson());
        }
        if(repairOrder.getNeedReturnVisit().equals("2")){
            repairOrder.setNeedVisitMes("不需要");
        } else {
            repairOrder.setNeedVisitMes("需要");
        }
        return repairOrder;
    }

    /**
     * 查询报修单列表
     *
     * @param repairOrder 报修单
     * @return 报修单
     */
    @Override
    public List<RepairOrder> selectRepairOrderList(RepairOrder repairOrder) {
        List<RepairOrder> list = repairOrderMapper.selectRepairOrderList(repairOrder);
        for (RepairOrder order : list) {
            if(StringUtils.isEmpty(order.getOwnerName())){
                order.setOwnerName(order.getRepairPerson());
            }
            continue;
        }
        return list;
    }

    /**
     * 新增报修单
     *
     * @param repairOrder 报修单
     * @return 结果
     */
    @Override
    public int insertRepairOrder(RepairOrder repairOrder) {
        Room room = roomService.selectRoomById(repairOrder.getRoomId());
        repairOrder.setId(IdUtils.simpleUUID());
        repairOrder.setFloorId(room.getFloorId());
        repairOrder.setCreateBy(SecurityUtils.getUsername());
        repairOrder.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isEmpty(repairOrder.getRepairPerson())){
            repairOrder.setRepairPerson(SecurityUtils.getUsername());
        }
        int count = repairOrderMapper.insertRepairOrder(repairOrder);
        if(count > 0 && !StringUtils.isEmpty(repairOrder.getRepairsId())){
            Repairs repairs = new Repairs();
            repairs.setId(repairOrder.getRepairsId());
            repairs.setHasApplyRepair(2);
            repairsMapper.updateRepairs(repairs);
        }
        this.setUnitInfo(repairOrder);
        return count;
    }

    /**
     * 修改报修单
     *
     * @param repairOrder 报修单
     * @return 结果
     */
    @Override
    public int updateRepairOrder(RepairOrder repairOrder) {
        this.setUnitInfo(repairOrder);
        repairOrder.setUpdateBy(SecurityUtils.getUsername());
        repairOrder.setUpdateTime(DateUtils.getNowDate());
        return repairOrderMapper.updateRepairOrder(repairOrder);
    }

    /**
     * 批量删除报修单
     *
     * @param ids 需要删除的报修单ID====关闭后台任务任务线程池====
     * @return 结果
     */
    @Override
    public int deleteRepairOrderByIds(String[] ids) {
        orgTreeService.deleteOrgTreeByIds(ids);
        return repairOrderMapper.deleteRepairOrderByIds(ids);
    }

    /**
     * 删除报修单信息
     *
     * @param id 报修单ID
     * @return 结果
     */
    @Override
    public int deleteRepairOrderById(String id) {
        orgTreeService.deleteOrgTreeById(id);
        return repairOrderMapper.deleteRepairOrderById(id);
    }


    private void setUnitInfo(RepairOrder bean) {
        Room parentBean =  roomService.selectRoomById(bean.getRoomId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(),parentBean.getRoomName() , bean.getCommunityId(), OrgConstants.repair, bean.getAncestors());
    }
}
